<template>
  <div class="singer" ref="singer">
    <list-view @select="selectSinger" :data="singers" ref="list"></list-view>
    <!--子页面-->
    <router-view></router-view>
  </div>
</template>

<script>
  import {getSingerList} from "../../api/singer";
  import {ERR_OK} from "../../api/config";
  import Singer from '../../common/js/singer'
  import ListView from "../../base/listview/listview"
  import {mapMutations} from 'vuex'
  import {playlistMixin} from "../../common/js/mixin";

  let _this = null;
  const HOT_NAME  = '热门';
  const HOT_NAME_LENGTH = 10;
  export default {
    mixins: [playlistMixin],
    props: [],
    data(){
      return {
        singers:[]
      }
    },
    mounted () {
      _this = this;
      _this.init();
      this._getSingerList()
    },
    methods: {
      init () {

      },
      handlePlaylist(playlist) {
        const bottom = playlist.length > 0 ? '60px' : ''
        this.$refs.singer.style.bottom = bottom
        this.$refs.list.refresh()
      },
      //配置路由跳转
      selectSinger(singer){
        this.$router.push({
          path:'/singer/'+singer.id
        })
        //相当于给mutation提交了commit ======  这样就给state.singer提交了singer数据
        this.setSinger(singer)
      },
      _getSingerList(){
        getSingerList().then((res) => {
          if(res.code === ERR_OK){
            this.singers = this._normalizeSingr(res.data.list);
            // console.log(this.singers)
          }
        })
      },
      //现将this.singers的数据存储起来，热门的是数据的前10条，其他的数据就按照顺序排序
      _normalizeSingr(list){
        // console.log(list)
        let map = {
          hot: {
            title: HOT_NAME,
            items: []
          }
        }
        list.forEach((item, index) => {
          // console.log(item)
          if (index < HOT_NAME_LENGTH) {
            map.hot.items.push(new Singer({
              name: item.Fsinger_name,
              id: item.Fsinger_mid
            }))
          }
          //a-z
          const key = item.Findex
          if (!map[key]) {
            map[key] = {
              title: key,
              items: []
            }
          }
          map[key].items.push(new Singer({
            name: item.Fsinger_name,
            id: item.Fsinger_mid
          }))
        })
        //为了得到有序数组，需要对map进行处理 ============ 热a-z
        let ret=[]
        let hot = []
        for(let key in map){
          let val = map[key]
          if(val.title.match(/[a-zA-Z]/)){
            ret.push(val)
          }else if(val.title === HOT_NAME){
            hot.push(val)
          }
        }
        //将按字母顺序对数组中的元素进行排序
        ret.sort((a,b) => {
          return a.title.charCodeAt(0) - b.title.charCodeAt(0)
        })
        //concat() 方法用于连接两个或多个数组
        return hot.concat(ret)
      },
      ...mapMutations({
        setSinger: 'SET_SINGER'
      })
    },
    watch: {},

    components: {
      ListView
    },
    computed: {},

  }
</script>


<style lang="stylus" scoped>
  @import '~@/common/stylus/mixin.styl';
  .singer
    position: fixed
    top: 88px
    bottom: 0
    width: 100%
</style>
